<template >
	<view class="behavior_box box">
		<!-- <view class="time_box height_44 bc_fff br_8">
			<view class="fz_16 fb fc_333">
				今日
			</view>
			<u-icon name="arrow-right" color="#666666"></u-icon>
		</view> -->
		<DateType :pickerValue="pickerValue" @updatePickerValue="updatePickerValue" />
		<SalesBehavior @oderTypeChange="oderTypeChange" :orderType="orderType" :managerList="managerList"
			:guideList="guideList" :goodsList="goodsList" :behaviorData="behaviorData" v-if="userType==1" />
		<!-- <SalesBehavior v-if="userType==1" /> -->
		<ManageBehavior :managerList="managerList" :guideList="guideList" :goodsList="goodsList"
			v-if="userType==2||userType==3" :userType="userType" />

	</view>
</template>

<script>
	import SalesBehavior from "./salesBehavior.vue"
	import ManageBehavior from "./managerBehavior.vue"
	import DateType from "./DateType.vue"
	import api from "@/service/config/api";
	import behaviorAnalysisVue from "./behaviorAnalysis.vue";
import { unix } from "moment";
	export default {
		components: {
			SalesBehavior,
			ManageBehavior,
			DateType
		},
		props: {
			userType: {
				default: 1,
				type: Number,
				dataView: {}
			},
			currentIndex: {
				default: 0,
				type: Number,
			},
		},
		data() {
			return {
				pickerValue: {
					type: 1,
					label: "今日",
					startTime: "",
					endTime: ""
				},
				behaviorData: {},
				managerList: [],
				guideList: [],
				goodsList: [],
				orderType: {
					id: 1,
					label: "分享商品次数"
				},
				timer: null

			}
		},
		wtach: {
			pickerValue: {
				handler(newValue, oldValue) {
					console.log("请求参数")
					console.log("新的请求")
					console.log(newValue)
					console.log(oldValue)
					if (newValue.type != oldValue.type&&this.userType!=4) {
						this.getBehaviorData(this.pickerValue)
					}
				},
				deep: true
			}
		},
		created() {
			console.log("渲染组件")
			console.log("获取行为分析数据")
			if (this.userType!=4) {
				console.log("在这里发起请求")
				this.getBehaviorData(this.pickerValue)
				this.timer = setInterval(() => {
					this.getBehaviorData(this.pickerValue)
				}, 10000)
			}
			
		},
		destroyed() {
			clearInterval(this.timer)
		},
		methods: {
			oderTypeChange(value) {
				console.log("切换类型")
				console.log(value)
				this.orderType = value
				this.getBehaviorData(this.pickerValue)
			},
			updatePickerValue(value) {
				console.log("父组件的值")
				console.log(value)
				if (value.type == 5) {
					if(value.startTime&&value.endTime) {
						if (new Date(value.startTime)>new Date(value.endTime)) {
							uni.showToast({
								title:"开始时间不能大于结束时间",
								icon:"none",
								duration:1500
							})
							return
						}
					}
					if (value.startTime && value.endTime && (value.startTime != this.pickerValue.startTime || value
							.endTime != this.pickerValue.endTime)) {

						this.getBehaviorData(value)
					}
				} else if (value.type != this.pickerValue.type) {
					this.getBehaviorData(value)
				}
				this.pickerValue = value
				

				// if (value.type != this.pickerValue.type ) {
				// 	this.pickerValue = value
				// 	this.getBehaviorData()
				// }



				console.log(this.pickerValue)
			},
			getBehaviorData(value) {
				console.log("参数值")
				console.log(this.pickerValue)
				api.post("/user/api/radar/behaviorAnalysisData", {
					current: 1,
					size: 10,
					type: value.type!=5?value.type:"",
					orderType: this.orderType.id,
					beginTime: value.startTime,
					endTime: value.endTime
				}).then(res => {
					this.behaviorData = res.data
					this.managerList = res.data.managerList
					this.guideList = res.data.guideList
					this.goodsList = res.data.goodsList
					console.log("获取行为分析数据")
					console.log(res)
					console.log(this.behaviorData)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.behavior_box {
		background: #F6F6F6;
		padding: 0 16rpx 24rpx 16rpx;
		// @include set_padding(12rpx, 16rpx);

		.time_box {
			@include set_padding(0rpx, 16rpx) @include flex_box(row, nowrap, space-between, center);
		}
	}
</style>